package mythesis.controller;
import javafx.scene.*;
import mythesis.controller.FormXML;
import mythesis.controller.VariableManager;
import mythesis.gui.main.components.api.TilePopupButton;
import mythesis.tiles.character.*;
import mythesis.tiles.variable.*;


var fxml:FormXML = new FormXML;
var numList:String[];
var wordList:String[];
var truthList:String[];
var candidateList:String[];

public function read(content:Node[], eventName:String, vm:VariableManager):String
{
    fxml.newXML();
    fxml.startEvent(eventName);
	
    readTiles(content);
	readVars(eventName, vm);

    fxml.endEvent();
    return fxml.getXML();
}

function readVars(eventName:String, vm:VariableManager)
{
	numList = vm.getUserVariables_Number(eventName);
	wordList = vm.getUserVariables_Word(eventName);
	truthList = vm.getUserVariables_Truth(eventName);
	candidateList = vm.getCandidateVariables(eventName);

	for (i in [0..numList.size()-1])
	{
	    fxml.addNumber(eventName, numList[i]);
	}
	
	for (i in [0..wordList.size()-1])
	{
		fxml.addWord(eventName, wordList[i]);
	}
	
	for (i in [0..truthList.size()-1])
	{
	    fxml.addTruth(eventName, truthList[i]);
	}
	for (i in [0..candidateList.size()-1])
	{
	    fxml.addCandidate(eventName, candidateList[i]);
	}
	
}


function readTiles(content:Node[])
{
    for (i in [0..content.size()])
    {
		/***************************************
		Basic Arithmetics (+,-,*,/,^)
          dataType[0]  is Number ONLY. 
          dataValue[0] is variable ONLY.
          dataValue[1] is Number ONLY.
          dataType[1]  is either constant OR variable.
		***************************************/
        if (content[i] instanceof Increase)
        {
            var line     = {content[i] as Increase}.line;
            var dtext:String[];
            var dtype:Number[];
            var dvalue:Number[];
            dtext[0]   = {content[i] as Increase}.args[0];
            dtext[1]   = {content[i] as Increase}.args[1];
            dtype[0]   = {content[i] as Increase}.dataType[0];
            dtype[1]   = {content[i] as Increase}.dataType[1];
            dvalue[0]  = {content[i] as Increase}.dataValue[0];
            dvalue[1]  = {content[i] as Increase}.dataValue[1];

            var addend:String;
            if      (dvalue[1] == 1){ addend = dtext[1]; }	// constant
            else if (dvalue[1] == 2){ addend = dtext[1]; }	// variable
            //println("	{dtext[0]} += {addend};");
            fxml.addTile(line, "Increase", dtext, dtype, dvalue);
            
        }
        else if (content[i] instanceof Decrease)
        {
            var line     = {content[i] as Decrease}.line;
            var dtext:String[];
            var dtype:Number[];
            var dvalue:Number[];
            dtext[0]   = {content[i] as Decrease}.args[0];
            dtext[1]   = {content[i] as Decrease}.args[1];
            dtype[0]   = {content[i] as Decrease}.dataType[0];
            dtype[1]   = {content[i] as Decrease}.dataType[1];
            dvalue[0]  = {content[i] as Decrease}.dataValue[0];
            dvalue[1]  = {content[i] as Decrease}.dataValue[1];

            var subtrahend:String;
            if      (dvalue[1] == 1){ subtrahend = dtext[1]; }	// constant
            else if (dvalue[1] == 2){ subtrahend = dtext[1]; }	// variable
            //println("	{dtext[0]} -= {subtrahend};");
            fxml.addTile(line, "Decrease", dtext, dtype, dvalue);
        }
        else if (content[i] instanceof Multiply)
        {
            var line     = {content[i] as Multiply}.line;
            var dtext:String[];
            var dtype:Number[];
            var dvalue:Number[];
            dtext[0]   = {content[i] as Multiply}.args[0];
            dtext[1]   = {content[i] as Multiply}.args[1];
            dtype[0]   = {content[i] as Multiply}.dataType[0];
            dtype[1]   = {content[i] as Multiply}.dataType[1];
            dvalue[0]  = {content[i] as Multiply}.dataValue[0];
            dvalue[1]  = {content[i] as Multiply}.dataValue[1];

            var multiplicand:String;
            if      (dvalue[1] == 1){ multiplicand = dtext[1]; }	// constant
            else if (dvalue[1] == 2){ multiplicand = dtext[1]; }	// variable
            //println("	{dtext[0]} *= {multiplicand};");
            fxml.addTile(line, "Multiply", dtext, dtype, dvalue);
        }
        else if (content[i] instanceof Divide)
        {
            var line     = {content[i] as Divide}.line;
            var dtext:String[];
            var dtype:Number[];
            var dvalue:Number[];
            dtext[0]   = {content[i] as Divide}.args[0];
            dtext[1]   = {content[i] as Divide}.args[1];
            dtype[0]   = {content[i] as Divide}.dataType[0];
            dtype[1]   = {content[i] as Divide}.dataType[1];
            dvalue[0]  = {content[i] as Divide}.dataValue[0];
            dvalue[1]  = {content[i] as Divide}.dataValue[1];

            var dividend:String;
            if      (dvalue[1] == 1){ dividend = dtext[1]; }	// constant
            else if (dvalue[1] == 2){ dividend = dtext[1]; }	// variable
            //println("	{dtext[0]} /= {dividend};");
            fxml.addTile(line, "Divide", dtext, dtype, dvalue);
        }
        else if (content[i] instanceof Raise)
        {
            var line     = {content[i] as Raise}.line;
            var dtext:String[];
            var dtype:Number[];
            var dvalue:Number[];
            dtext[0]   = {content[i] as Raise}.args[0];
            dtext[1]   = {content[i] as Raise}.args[1];
            dtype[0]   = {content[i] as Raise}.dataType[0];
            dtype[1]   = {content[i] as Raise}.dataType[1];
            dvalue[0]  = {content[i] as Raise}.dataValue[0];
            dvalue[1]  = {content[i] as Raise}.dataValue[1];

            var raiser:String;
            if      (dvalue[1] == 1){ raiser = dtext[1]; }	// constant
            else if (dvalue[1] == 2){ raiser = dtext[1]; }	// variable
            //println("	{dtext[0]} ^= {raiser};");
            fxml.addTile(line, "Raise", dtext, dtype, dvalue);
        }
		/***************************************
		Boolean methods (+,-,*,/,^)
          dataType[0]  is Truth ONLY. 
          dataValue[0] is Variable ONLY.
		***************************************/
        else if (content[i] instanceof Testify)
        {
            var line     = {content[i] as Testify}.line;
            var dtext:String[];
            var dtype:Number[];
            var dvalue:Number[];
            dtext[0]   = {content[i] as Testify}.args[0];
            dtype[0]   = {content[i] as Testify}.dataType[0];
            dvalue[0]  = {content[i] as Testify}.dataValue[0];

            var truth:String;
            //println("	{dtext} = true;");
            fxml.addTile(line, "Testify", dtext, dtype, dvalue);
        }
        else if (content[i] instanceof Falsify)
        {
            var line     = {content[i] as Falsify}.line;
            var dtext:String[];
            var dtype:Number[];
            var dvalue:Number[];
            dtext[0]   = {content[i] as Falsify}.args[0];
            dtype[0]   = {content[i] as Falsify}.dataType[0];
            dvalue[0]  = {content[i] as Falsify}.dataValue[0];

            var truth:String;
            //println("	{dtext} = false;");
            fxml.addTile(line, "Falsify", dtext, dtype, dvalue);
        }
		/***************************************
		Conditional methods (+,-,*,/,^)
          dataType[0]  is Number, Word or Truth ONLY. 
          dataValue[0] is Variable ONLY.
          dataType[1]  is Special ONLY. 
          dataValue[1] is Special ONLY.
          dataType[2]  is is same as dataType[0] 
          dataValue[2] is Constant or Varialbe ONLY.
		***************************************/
        else if (content[i] instanceof If)
        {
            var line     = {content[i] as If}.line;
            var dtext:String[];
            var dtype:Number[];
            var dvalue:Number[];
            dtext[0]   = {content[i] as If}.args[0];
            dtype[0]   = {content[i] as If}.dataType[0];
            dvalue[0]  = {content[i] as If}.dataValue[0];
            dtext[1]   = {content[i] as If}.args[1];
            dtype[1]   = {content[i] as If}.dataType[1];
            dvalue[1]  = {content[i] as If}.dataValue[1];
            dtext[2]   = {content[i] as If}.args[2];
            dtype[2]   = {content[i] as If}.dataType[2];
            dvalue[2]  = {content[i] as If}.dataValue[2];
            //println("line: {line}\ndtext1:{dtext[0]}\ndtype1:{dtype[0]}\ndvalue1:{dvalue[0]}\n");

            var truth:String;
            // number ---
            if (dtype[0] == 1)
            {
				//println("	if ({dtext[0]} {dtext[1]} {dtext[2]}) \{");                
            }
            // word ---
            else if (dtype[0] == 2)
            {
             	var comp = "errword in IF";
             	if (dvalue[2] == 1)
             	{
             	    comp = "\"{dtext[2]}\"";
             	}
             	else if (dvalue[2] == 2)
             	{
             	    comp = dtext[2];
             	}
             	
                if (dtext[1] == "==")
                {
                    //println("	if ({dtext[0]}.equals({comp})) \{");
                }
                else if (dtext[1] == "!=")
                {
                    //println("	if (!{dtext[0]}.equals({comp})) \{");
                }
            }
            // truth ---
            else if (dtype[0] == 3)
            {
                //println("	if ({dtext[0]} {dtext[1]} {dtext[2]}) \{");
            }
            
            fxml.addTile(line, "If", dtext, dtype, dvalue);
        }
        else if (content[i] instanceof While)
        {
            var line     = {content[i] as While}.line;
            var dtext:String[];
            var dtype:Number[];
            var dvalue:Number[];
            dtext[0]   = {content[i] as While}.args[0];
            dtype[0]   = {content[i] as While}.dataType[0];
            dvalue[0]  = {content[i] as While}.dataValue[0];
            dtext[1]   = {content[i] as While}.args[1];
            dtype[1]   = {content[i] as While}.dataType[1];
            dvalue[1]  = {content[i] as While}.dataValue[1];
            dtext[2]   = {content[i] as While}.args[2];
            dtype[2]   = {content[i] as While}.dataType[2];
            dvalue[2]  = {content[i] as While}.dataValue[2];
            //println("line: {line}\ndtext1:{dtext[0]}\ndtype1:{dtype[0]}\ndvalue1:{dvalue[0]}\n");

            var truth:String;
            // number ---
            if (dtype[0] == 1)
            {
				//println("   int i=0;\n   while({dtext[0]} {dtext[1]} {dtext[2]}) \{\n   if ((++i)==100)\{break;\}\n");                
            }
            // word ---
            else if (dtype[0] == 2)
            {
             	var comp = "errword in WHILE";
             	if (dvalue[2] == 1)
             	{
             	    comp = "\"{dtext[2]}\"";
             	}
             	else if (dvalue[2] == 2)
             	{
             	    comp = dtext[2];
             	}
             	
                if (dtext[1] == "==")
                {
                    //println("   int i=0;\n   while({dtext[0]}.equals({comp}))\{\n   if((++i)==100)\{break;\}\n");
                }
                else if (dtext[1] == "!=")
                {
                    //println("   int i=0;\n   while(!{dtext[0]}.equals({comp}))\{\n   if((++i)==100)\{break;\}\n");
                }
            }
            // truth ---
            else if (dtype[0] == 3)
            {
                //println("   int i=0;\n   while({dtext[0]} {dtext[1]} {dtext[2]}) \{\n   if ((++i)==100)\{break;\}\n");
            }
            fxml.addTile(line, "While", dtext, dtype, dvalue);
        }
        else if (content[i] instanceof EndCondition)
        {
            var line     = {content[i] as EndCondition}.line;
            var dtext:String[] = [];
            var dtype:Number[] = [];
            var dvalue:Number[] = [];

            var truth:String;
            fxml.addTile(line, "EndCondition", dtext, dtype, dvalue);
        }
        else if (content[i] instanceof BreakCondition)
        {
            var line     = {content[i] as BreakCondition}.line;
            var dtext:String[] = [];
            var dtype:Number[] = [];
            var dvalue:Number[] = [];

            var truth:String;
            fxml.addTile(line, "BreakCondition", dtext, dtype, dvalue);
        }
		/***************************************
		Var declare and set(+,-,*,/,^)
          dataType[0]  is Number, Word or Truth ONLY. 
          dataValue[0] is Variable ONLY.
          dataType[1]  is Special ONLY. 
          dataValue[1] is Special ONLY.
          dataType[2]  is is same as dataType[0] 
          dataValue[2] is Constant or Varialbe ONLY.
		***************************************/
        else if (content[i] instanceof Note)
        {
            var line     = {content[i] as Note}.line;
            var dtext:String[];
            var dtype:Number[];
            var dvalue:Number[];
            dtext[0]   = {content[i] as Note}.args[0];
            dtext[1]   = {content[i] as Note}.args[1];
            dtype[0]   = {content[i] as Note}.dataType[0];
            dtype[1]   = {content[i] as Note}.dataType[1];
            dvalue[0]  = {content[i] as Note}.dataValue[0];
            dvalue[1]  = {content[i] as Note}.dataValue[1];
            
            
            //--- integer
            if (dtext[1] == "Number")
            {
                //println(" int {dtext[0]} = 0;");
            }
            else if (dtext[1] == "Word")
            {
                //println(" String {dtext[0]} = \"\";");
            }
            else if (dtext[1] == "Truth")
            {
                //println(" boolean {dtext[0]} = false;");
            }
            fxml.addTile(line, "Note", dtext, dtype, dvalue);
        }
        else if (content[i] instanceof Set)
        {
            var line     = {content[i] as Set}.line;
            var dtext:String[];
            var dtype:Number[];
            var dvalue:Number[];
            dtext[0]   = {content[i] as Set}.args[0];
            dtext[1]   = {content[i] as Set}.args[1];
            dtype[0]   = {content[i] as Set}.dataType[0];
            dtype[1]   = {content[i] as Set}.dataType[1];
            dvalue[0]  = {content[i] as Set}.dataValue[0];
            dvalue[1]  = {content[i] as Set}.dataValue[1];

            //--- integer
            if (dtype[0] == 1)
            {
                //println(" {dtext[0]} = {dtext[1]};");
            }
            //--- word
            else if (dtype[0] == 2)
            {
                var assign = "";
                if (dvalue[1] == 1)
                {
                    assign = "\"{dtext[1]}\"";
                }
                else if (dvalue[1] == 2)
                {
                    assign = "{dtext[1]}";
                }
                //println(" {dtext[0]} = {assign};");
            }
            //--- truth
            else if (dtype[0] == 3)
            {
                //println(" {dtext[0]} = {dtext[1]};");
            }
            fxml.addTile(line, "Set", dtext, dtype, dvalue);
        }
		/***************************************
		Characteer action tiles
          various settings!
		***************************************/
        else if (content[i] instanceof RollDice)
        {
            var line     = {content[i] as RollDice}.line;
            var dtext:String[] = [];
            var dtype:Number[] = [];
            var dvalue:Number[] = [];

			//println("<<ROLLDICE>>");
            fxml.addTile(line, "RollDice", dtext, dtype, dvalue);
        }
        else if (content[i] instanceof Suggest)
        {
            var line     = {content[i] as Suggest}.line;
            var dtext:String[];
            var dtype:Number[];
            var dvalue:Number[];
            dtext[0]   = {content[i] as Suggest}.args[0];
            dtext[1]   = {content[i] as Suggest}.args[1];
            dtype[0]   = {content[i] as Suggest}.dataType[0];
            dtype[1]   = {content[i] as Suggest}.dataType[1];
            dvalue[0]  = {content[i] as Suggest}.dataValue[0];
            dvalue[1]  = {content[i] as Suggest}.dataValue[1];

            //println("<<SUGGEST>>");
            fxml.addTile(line, "Suggest", dtext, dtype, dvalue);            
        }
        else if (content[i] instanceof Accuse)
        {
            var line     = {content[i] as Accuse}.line;
            var dtext:String[];
            var dtype:Number[];
            var dvalue:Number[];
            dtext[0]   = {content[i] as Accuse}.args[0];
            dtext[1]   = {content[i] as Accuse}.args[1];
            dtext[2]   = {content[i] as Accuse}.args[2];
            dtype[0]   = {content[i] as Accuse}.dataType[0];
            dtype[1]   = {content[i] as Accuse}.dataType[1];
            dtype[2]   = {content[i] as Accuse}.dataType[2];
            dvalue[0]  = {content[i] as Accuse}.dataValue[0];
            dvalue[1]  = {content[i] as Accuse}.dataValue[1];
            dvalue[2]  = {content[i] as Accuse}.dataValue[2];

            //println("<<ACCUSE>>");
            fxml.addTile(line, "Accuse", dtext, dtype, dvalue);            
        }
        else if (content[i] instanceof ListenToSuggestion)
        {
            var line     = {content[i] as ListenToSuggestion}.line;
            var dtext:String[] = [];
            var dtype:Number[] = [];
            var dvalue:Number[] = [];

			//println("<<LISTENTOSUGGESTION>>");
            fxml.addTile(line, "ListenToSuggestion", dtext, dtype, dvalue);
        }
        else if (content[i] instanceof DisproveSuggestionWith)
        {
            var line   = {content[i] as DisproveSuggestionWith}.line;
            var dtext:String[];
            var dtype:Number[];
            var dvalue:Number[];
            dtext[0]   = {content[i] as DisproveSuggestionWith}.args[0];
            dtype[0]   = {content[i] as DisproveSuggestionWith}.dataType[0];
            dvalue[0]  = {content[i] as DisproveSuggestionWith}.dataValue[0];
            
            //println("<<DISPROVESUGGESTION>>");
            fxml.addTile(line, "DisproveSuggestionWith", dtext, dtype, dvalue);            
        }
        else if (content[i] instanceof GoTo)
        {
            var line     = {content[i] as GoTo}.line;
            var dtext:String[];
            var dtype:Number[];
            var dvalue:Number[];
            dtext[0]   = {content[i] as GoTo}.args[0];
            dtype[0]   = {content[i] as GoTo}.dataType[0];
            dvalue[0]  = {content[i] as GoTo}.dataValue[0];
            
            //println("<<GOTO>>");
            fxml.addTile(line, "GoTo", dtext, dtype, dvalue);            
        }
    }
}


public class CodeReader
{
}	